//引入express模块
const express=require('express')
//创建路由器对象
const r=express.Router();
//引入数据库连接池
const pool=require('../pool.js')
const { post } = require('./user.js');

// 添加课程接口
r.post("/addcourse",(req,res)=>{
	console.log(req.body)
	let sql='insert into course_tab (course_title,course_chapter,course_type,course_hourse,course_studyNumber,course_brief,course_tutor,course_image,course_fees,course_teach,course_video,course_detail_img) value(?,?,?,?,?,?,?,?,?,?,?,?)'
	pool.query(sql,[
	req.body.course_title,
	req.body.course_chapter,
	req.body.course_type,
	req.body.course_hourse,
	req.body.course_studyNumber,
	req.body.course_brief,
	req.body.course_tutor,
	req.body.course_image,
	req.body.course_fees,
	req.body.course_teach,
	req.body.course_video,
	req.body.course_detail_img],(err,result)=>{
		if(err)return;//阻止往后执行
	})
	res.send({code:200,msg:'添加成功'})
})
//删除课程接口
r.delete('/remove',(req,res)=>{
	console.log(req.query.course_id)
	pool.query('delete from course_tab where course_id=?',req.query.course_id, (err, result) => {
		if (err) return;
		res.send({
			code: 200,
			msg: '删除成功'
		})
	})
})
//创建首页数据接口
r.get('/course',(req,res)=>{
	pool.query('select * from course_tab where course_id>=0 and course_id<=6',(err,result)=>{
		if(err)throw err
		console.log(result)
		res.send(result)
	})
	
})
r.get('/course_1',(req,res)=>{
	pool.query('select * from course_tab where course_id>=7 and course_id<=8',(err,result)=>{
		if(err)throw err
		console.log(result)
		res.send(result)
	})
	
})
//获取课程详情数据接口
r.get('/detail',(req,res)=>{
	const id=parseInt(req.query.lid) 
	console.log(typeof(id))
	pool.query('select * from course_tab where course_id=?',id,(err,result)=>{
		if(err)throw err
		res.send(result)
	})
})
//获取讲师介绍数据接口
r.get('/teacher',(req,res)=>{
	const id=req.query.lid
	console.log(id)
	pool.query('select * from tutor_tab where tutor_id=?',id,(err,result)=>{
		if(err)throw err
		res.send(result)
	})
})
//获取课程视频数据请求接口
r.get("/course_v",(req,res)=>{
	const id=req.query.lid
	console.log(id)
	pool.query('select * from course_tab where course_id=?',id,(err,result)=>{
		if(err)throw err
		res.send(result)
	})
})
//获取免费课课程接口
r.get('/course_mianfei',(req,res)=>{
	console.log(req.query.p);
	let p=req.query.p
	p=p?parseInt(p):0
	sql='select * from course_tab limit ?,6'
	pool.query(sql,p,(err,result)=>{
		if(err)throw err
		// console.log(result)
		res.send(result)
	})	
})
//获取实战课课课程接口
r.get('/course_shizhan',(req,res)=>{
	console.log(req.query.p);
	let p=req.query.p
	p=p?parseInt(p):0
	p+=17
	sql='select * from course_tab limit ?,8'
	pool.query(sql,p,(err,result)=>{
		if(err)throw err
		// console.log(result)
		res.send(result)
	})	
})
//获取搜索框查询课程数据接口
r.get('/course_input',(req,res)=>{
	console.log(req.query.keyword)
	let title=`%${req.query.keyword}%`
	let sql=`select * from course_tab where course_title like ?`
	pool.query(sql,title,(err,result)=>{
		if(err)return err
		res.send(result)
	})
})
//购物车添加课程接口
r.post('/shopping_car',(req,res,next)=>{
	//1.1获取post传递的参数
	let obj=req.body;
	console.log(obj);
	pool.query('insert into shopping_car (course_title,course_image,course_fees,course_id) values(?,?,?,?)',[obj.course_title,obj.course_image,obj.course_fees,obj.course_id],(err,result)=>{
		console.log(err)
		if(err){
			return;//阻止往后执行
		}
		res.send({code:200,msg:'添加成功'});
	});
});
//购物车页面获取数据接口
r.get('/course_find',(req,res)=>{
	// console.log(req.query)
	pool.query('select * from shopping_car',(err,result)=>{
		console.log(result)
		if(err)return err
		res.send(result)
	})
})
//购物车页面数据删除请求
r.post('/course_delete',(req,res)=>{
	console.log(req.query.lid)
	pool.query('delete from shopping_car where course_id=?',req.query.lid,(err,result)=>{
		if(err)return ;
		res.send({code:200,msg:'删除成功'})
	})
})
//订单页面获取购物车表的数据
r.get('/course_ord_find',(req,res)=>{

	let p=req.query.p
	// console.log(p)
	strs=p.split(',')
	strs_1=[];
	strs.forEach(function(item,index,arr){ 	
		strs_1.push(parseInt(item))
	})
		// console.log(strs_1)
	sql=`select * from shopping_car where  course_id in (${strs_1})`
	//此处sql有问题
	console.log (sql)	
	pool.query(sql,(err,result)=>{
		if(err)return;
		console.log(result)
		res.send(result)
	})
})
//用户扣费接口
r.get('/user_price',(req,res)=>{
	let i=parseInt(req.query.id)
	let p=parseInt(req.query.price)
	console.log(i)
	console.log(p)
	sql='update stu_tab set stu_balance=stu_balance-? where stu_id=?'
	pool.query(sql,[p,i],(err,result)=>{
		if(err)return;
		res.send({code:200,msg:'支付成功'})
	})
})
//课程存入订单表接口
r.post('/orders',(req,res)=>{
	let l=req.query.l;
	strs=l.split(',')
	strs_2=[];
	strs.forEach(function(item,index,arr){ 	
		strs_2.push(parseInt(item))
	})
	console.log(strs_2)
	sql=`insert into orders_tab(orders_id,course_title,course_img,commodity_Price) select course_id,course_title,course_image,course_fees from shopping_car where course_id in (${strs_2})`
	pool.query(sql,(err,result)=>{
		console.log(err)
		if(err)return
		res.send('课程插入成功')
	})
})
//个人中心页面查询订单表数据接口
r.get('/orderfind',(req,res)=>{
	pool.query('select * from orders_tab',(err,result)=>{
		console.log(result)
		if(err){
			return
		}
		res.send(result)
	})
})
//从学生信息表中获取账户余额请求
r.get('/price',(req,res)=>{
	console.log(parseInt(req.query.stu_id))
	let stu_id=parseInt(req.query.stu_id)
	pool.query('select * from stu_tab where stu_id=?',stu_id,(err,result)=>{
		if(err){
			return
		}
		res.send(result)
	})
})
//将聊天内容插入到聊天表请求
r.post('/chat',(req,res)=>{
	console.log(req.body.content)
	console.log(req.body.stu_name)
	pool.query('insert into char_tab(name,content) values(?,?)',[req.body.stu_name,req.body.content],(err,result)=>{
		if(err){
			return;
		}
		res.send({
			code:200,
			msg:'插入成功'
		})
	})
})
r.post('/chat_1',(req,res)=>{
	console.log(req.body.content)
	console.log(req.body.tutor_name)
	pool.query('insert into char_tab(name,content) values(?,?)',[req.body.tutor_name,req.body.content],(err,result)=>{
		if(err){
			return;
		}
		res.send({
			code:200,
			msg:'插入成功'
		})
	})
})
//查询聊天内容请求
r.get('/select_content',(req,res)=>{
	pool.query('select * from char_tab',(err,result)=>{
		if(err){
			return
		}
		res.send(result)
	})
})
//查询课程接口
r.get('/selectCourse', (req, res) => {
	sql = 'select * from course_tab'
	pool.query(sql, (err, result) => {
		if (err) {
			return
		} else {
			res.send({
				result
			})
		}
	})
})
//查询订单接口
r.get('/selectOrders',(req,res)=>{
	sql='select * from orders_tab'
	pool.query(sql,(err,result)=>{
		if(err){
			return
		}else{
			res.send({
				result
			})
		}
	})
})
//删除订单接口
r.delete('/deleteOrders', (req, res) => {
	console.log(req.query.id)
	let id = req.query.id
	pool.query('delete from orders_tab where id=?',id, (err, result) => {
		if (err) return;
		res.send({
			code: 200,
			msg: '删除成功'
		})
	})
})


// 导出路由器对象
module.exports=r;